package cn.edu.cimv.controller;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.dao.entity.Project;
import cn.edu.cimv.pojo.Message;
import cn.edu.cimv.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


/**
 * Created by 门铎 on 2016/8/2.
 */
@Controller
@RequestMapping("/cimv/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private Message message;

    /**
     * 后台 list project
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(Model model){
        model.addAttribute("projectList",projectService.queryProject());
        return "project/list";
    }

    /**
     * 跳转到新建项目页
     * @return
     */
    @RequestMapping("new")
    public String projectNew(){
        return "project/new";
    }

    /**
     * 创建新的项目的动作
     * @param project
     * @return
     */
    @RequestMapping( value = "create", method = RequestMethod.POST)
    public @ResponseBody Message create(Project project) {
        projectService.editProject(project);
        message.setMsg(InfoConstant.CREATE_NORMAL_SUCCESS);
        return message;
    }

    /**
     * 按照id删除项目
     * @param id
     * @return
     */
    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    public @ResponseBody Message delete(@PathVariable("id")int id){
        projectService.deleteProject(id);
        message.setMsg(InfoConstant.DELETE_NORMAL_SUCCESS);
        return message;
    }

   /* *//**
     * 废弃了
     * @param id
     * @param flag
     * @return
     *//*
    @RequestMapping(value = "setLast/{id}/{flag}",method = RequestMethod.GET)
    public @ResponseBody Message setLast(@PathVariable("id")int id,@PathVariable("flag")int flag){
        projectService.setLastProject(id,flag);
        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;
    }*/

    /**
     * 跳转到更新页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updatePage(@PathVariable("id")int id,Model model){
        model.addAttribute("project",projectService.queryProjectById(id));
        return "project/update";
    }

    /**
     * 执行更新的动作
     * @param project
     * @return
     */
    @RequestMapping(value = "updateProject/{id}",method = RequestMethod.POST)
    public @ResponseBody Message updateProject(Project project){
        //更新
        projectService.updateProject(project);

        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;

    }
}
